package modules.webui.view
{
	import br.com.stimuli.loading.BulkLoader;
	import br.com.stimuli.loading.BulkProgressEvent;
	import br.com.stimuli.loading.loadingtypes.URLItem;
	
	import flash.events.Event;
	import flash.events.HTMLUncaughtScriptExceptionEvent;
	import flash.events.KeyboardEvent;
	import flash.filesystem.File;
	import flash.html.*;
	import flash.ui.Keyboard;
	
	import jui.model.Config;
	
	import modules.webui.control.LinkControls;
	
	import mx.controls.HTML;
	import mx.events.FlexEvent;
	import mx.modules.Module;
	
	import spark.components.TextInput;
	
	public class WebUIModuleBase extends Module
	{
		public var html: HTML;
		public var address: TextInput;
		public function WebUIModuleBase()
		{
			super();
		}
		
		protected function windowedapplicationbase1_creationCompleteHandler(event:FlexEvent):void
		{
			gotoAddress(Config.getConfig().startWebLocation);
		}
		
		protected function addressBar_keyUpHandler(event:KeyboardEvent):void
		{
			if(event.keyCode == Keyboard.ENTER)
			{
				gotoAddress(address.text);
			}
		}
		
		public function gotoAddress($address: String): void
		{
			if ($address != "")
			{
				var locationUri: String = $address;
				if (locationUri.indexOf("http://") == -1)
				{
					locationUri = "http://" + locationUri;
				}
				html.location = locationUri;
				address.text = locationUri;
			}
		}
		
		protected function htmlView_completeHandler(event:Event):void
		{
			trace("-- html load complete ");
			
			loadJavascriptFiles();
		}

		private function loadJavascriptFiles(): void
		{
			var bl: BulkLoader = BulkLoader.getLoader("jsBulkLoader");
			if (bl == null)
			{
				bl = new BulkLoader("jsBulkLoader")
			}
			
			var fl: File = new File()
			var basePath: String = File.applicationDirectory.nativePath; 
			var pthJQuery: String = fl.resolvePath( basePath + '/modules/webui/javascript/libs/jquery-1.3.2.js').nativePath;
			var pthMain: String = fl.resolvePath( basePath + '/modules/webui/javascript/fr/main.js').nativePath;
			
			bl.addEventListener(BulkProgressEvent.COMPLETE, jsFileCompleteHandler );
			bl.add( pthJQuery );
			bl.add( pthMain );
			bl.start();
		}
		
		private function jsFileCompleteHandler(event: BulkProgressEvent):void
		{
			var bl: BulkLoader = BulkLoader.getLoader("jsBulkLoader");
			
			var dom: Object = html.domWindow; 
			var d: Object = dom.document;
			var de: Object = d.documentElement;
			var body: Object = de.childNodes[1];

			dom.linkControls = new LinkControls();
		
			var items: Array = bl.items.concat();
			
			for each(var loadedItem:URLItem in items )
			{
				var loadedJSCode: String = loadedItem.content;
				setJsCode(loadedJSCode);
			}
		}
		
		private function setJsCode(value: String):void
		{
			var dom: Object = html.domWindow;
			var jsCode: String = value;
			dom.eval(jsCode);
		}
		
		protected function htmlView_htmlDOMInitializeHandler(event:Event):void
		{
			trace("-- htmlDOMInitialize ");	
		}
		
		protected function htmlView_uncaughtScriptExceptionHandler(event:HTMLUncaughtScriptExceptionEvent):void
		{
			trace("-- uncaughtScriptException ");
		}		
	}
}